package com.lzx.paymentServer.controller;


import com.lzx.commons.entity.Payment;
import com.lzx.commons.entity.Result;
import com.lzx.commons.utils.ResultUtil;
import com.lzx.paymentServer.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private PaymentService paymentService;

    @GetMapping("/get")
    public Result get(@RequestParam("id") Integer id) throws InterruptedException {
//        Thread.sleep(1000);
        return ResultUtil.customResultParam("200","success:"+serverPort,paymentService.getById(id));
    }

    @GetMapping("/list")
    public Result list(){
        return ResultUtil.customResultParam("200","success:"+serverPort,paymentService.list());
    }


    @PostMapping("/save")
    public Result save(@RequestBody Payment payment){
        return ResultUtil.customResultParam("200","success:"+serverPort,paymentService.save(payment));
    }

    @GetMapping("/services")
    public Object discover(){
        List<String> services = discoveryClient.getServices();
        services.forEach(System.out::println);
        List<ServiceInstance> paymentserver = discoveryClient.getInstances("PAYMENTSERVER");
        paymentserver.forEach(serviceInstance -> {
            System.out.println(serviceInstance.getHost()+"\t"+serviceInstance.getPort()+"\t"+serviceInstance.getUri());
        });
        return discoveryClient;
    }



}
